package leo.game.ui
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.StageScaleMode;
	import flash.system.ApplicationDomain;
	import flash.utils.getDefinitionByName;
	
	import leo.TDGame;
	import leo.data.GameConfig;
	import leo.data.GameConst;
	import leo.data.GameModel;
	import leo.game.stage.Game;
	import leo.manager.BitmapManager;
	import leo.manager.LoaderManager;
	import leo.manager.McMannager;

	/**
	 * 大loading界面 
	 * @author leo
	 * 
	 */	
	public class UIMainLoading extends Sprite
	{
		private static var _instance :UIMainLoading;
		private var _percent :int;
		/**
		 * 大loading界面 
		 */		
		private var bigLoadingMC :MovieClip;
		/**
		 * 进度条界面 
		 */		
		public var loadingMC :MovieClip;
		public function UIMainLoading()
		{
			super();
			init();
		}
		
		/**
		 *初始化 
		 * 
		 */		
		private function init():void
		{
			this.bigLoadingMC = McMannager.instance.getMCByName(GameConst.MC_LOADING);
			
			this.loadingMC = this.bigLoadingMC.loadingMC;
			this.loadingMC.gotoAndStop(1);
			this.loadingMC.num0.gotoAndStop(11);    //第11帧为不显示该数字
			this.loadingMC.num1.gotoAndStop(11);
			this.loadingMC.num2.gotoAndStop(10);    //第10帧为数字0
			addChild(this.bigLoadingMC);
			var imgList :Array = GameModel.instance.imgList;
			LoaderManager.instance.loadList(imgList);
		}

		public function get percent():int
		{
			return _percent;
		}
		/**
		 * 设置进度条百分比 
		 * @param value
		 * 
		 */
		public function set percent(value:int):void
		{
			value = value < 1 ? 1 : value > 100 ? 100 :value;
			_percent = value;
			if(loadingMC.currentFrame >= value) return;
			this.loadingMC.gotoAndStop(value);
			var str :String = value.toString();
			var ary :Array = str.split("");
			var num :int = 0;
			if(ary.length <= 1)
			{
				num = int(ary[0]);
				num = num > 0 ? num : 10;
				this.loadingMC.num0.gotoAndStop(11);
				this.loadingMC.num1.gotoAndStop(11);
				this.loadingMC.num2.gotoAndStop(num);
			}else if(ary.length <= 2)
			{
				this.loadingMC.num0.gotoAndStop(11);
				num = int(ary[0]);
				num = num > 0 ? num : 10;
				this.loadingMC.num1.gotoAndStop(num);
				num = int(ary[1]);
				num = num > 0 ? num : 10;
				this.loadingMC.num2.gotoAndStop(num);
			}else if(ary.length <= 3)
			{
				num = int(ary[0]);
				num = num > 0 ? num : 10;
				this.loadingMC.num0.gotoAndStop(num);
				num = int(ary[1]);
				num = num > 0 ? num : 10;
				this.loadingMC.num1.gotoAndStop(num);
				num = int(ary[2]);
				num = num > 0 ? num : 10;
				this.loadingMC.num2.gotoAndStop(num);
			}
			if(loadingMC.currentFrame >= 100)
			{
				trace("100%");
				destroy();
				

				initTowerBMD();
				initBulletBMD();
				TDGame.instance.setStage(Game.instance);

			}
		}
		/**
		 * 初始化防守塔的位图数据 
		 * 
		 */		
		private function initTowerBMD():void
		{
			var mcTowerAry :Array = GameConfig.mc_tower_ary;
			for(var i :int = 0; i < mcTowerAry.length; i++)
			{
				var name :String = mcTowerAry[i] as String;
				BitmapManager.instance.setBitmapdataInDict(name);
			}
			
			BitmapManager.instance.setBitmapdataInDict(GameConst.BASE);
		}
		/**
		 * 初始化子弹位图数据 
		 * 
		 */		
		private function initBulletBMD():void
		{
			var mcBulletAry :Array = GameConfig.mc_bullet_ary;
			for(var i :int = 0; i < mcBulletAry.length; i++)
			{
				var name :String = mcBulletAry[i] as String;
				BitmapManager.instance.setBitmapdataInDict(name);
			}
		}

		public static function get instance():UIMainLoading
		{
			if(_instance == null)
			{
				_instance = new UIMainLoading();
			}
			return _instance;
		}

		public function destroy():void
		{
			if(this)
			{
				this.removeChild(this.bigLoadingMC);
				this.bigLoadingMC = null;
			}
		}
		
	}
}